<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="utf-8">
    <title>模板列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" href="../../../layuiadmin/layui/css/layui.css"
          media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" href="../../../layuiadmin/style/admin.css"
          media="all">
    <script th:src="@{/js/hope.jquery.form.js}"></script>

</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label for="authorityNameEn" class="layui-form-label">名称（英）：</label>
                    <div class="layui-input-block">
                        <input type="text" id="authorityNameEn" name="authorityName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label for="authorityName" class="layui-form-label">名称（中）：</label>
                    <div class="layui-input-block">
                        <input type="text" id="authorityName" name="authorityName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label for="authorityDesc" class="layui-form-label">描述</label>
                    <div class="layui-input-block">
                        <input type="text" id="authorityDesc" name="authorityDesc" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin layui-hide" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-useradmin layui-hide" data-type="add">添加</button>
            </div>

            <table id="lay-table" lay-filter="lay-table"></table>
<!--            <script type="text/html" id="imgTpl">-->
<!--                <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>-->
<!--            </script>-->
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;

        //表格实例
        table.render({
            elem: '#lay-table'
            , url: '/system/authority/layGrid' //数据接口
            , page: true //开启分页
            , limit: 10
            , toolbar:'default'
            , cols: [
                [
                    {type: 'numbers', title: '序号', fixed: 'left'},
                    {field: 'authorityName', title: '授权名称'},
                    {field: 'authorityNameEn', title: '识别码'},
                    {field: 'authorityDesc', title: '描述'},
                    {
                        field: 'deleted', title: '是否删除',hide:true, templet: function (d) {
                            return d === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'createTime ', title: '添加日期',width: 120 ,templet: function (d) {
                            return layui.util.toDateString(d.createTime, "yyyy-MM-dd");
                        }
                    },
                    {title: "操作", templet: "#table_op", width: 220}
                ]]
        });

        //监听搜索
        form.on('submit(lay-table-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('lay-table', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('lay-table')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    layer.close(index);

                    layer.confirm('确定删除吗？', function (index) {

                        //执行 Ajax 后重载
                        /*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        */
                        layer.close(index);
                        table.reload('lay-table');
                        layer.msg('已删除');
                    });
                });
            }
            , add: function () {
                var indexAdd = layer.open({
                    type: 2
                    , title: '授权-添加'
                    , content: 'add.html'
                    , maxmin: true
                    , area: ['80%', '80%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var windowSubmitBtn = $('#layui-layer' + index + " a.layui-layer-btn0");
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'lay-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        iframeWindow.$("#myForm").ajaxSubmit({
                            beforeSubmit: function () {
                                layer.msg('数据提交中...', {time: 0});
                                //console.log(iframeWindow);
                                //$('#layui-layer' + index + " a.layui-layer-btn0").addClass("layui-btn layui-btn-disabled");
                                windowSubmitBtn.hide();
                            },
                            success: function (data) {
                                if (data.code === 0) {
                                    layer.close(indexAdd);
                                    layer.msg('保存成功');
                                    table.reload('lay-table');
                                } else {
                                    layer.alert('添加失败');
                                    windowSubmitBtn.show();
                                }
                            },
                            error: function (err) {
                                layer.alert(err.msg);
                                windowSubmitBtn.show();
                            }

                        });

                        /*iframeWindow.$("#myForm").ajaxSubmit(function(data){
                            if(data.code == 0){
                                layer.close(indexAdd);
                                layer.msg('保存成功');
                                table.render('lay-table');
                            }else{
                                layer.alert('添加失败');
                            }
                        });*/
                    }
                });
            }
        };

        //监听表格头部工具栏
        table.on('toolbar(lay-table)',function(obj){
            //var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    var indexAdd = layer.open({
                        type:2,
                        content:['add'],
                        maxmin:true,
                        area:['80%','80%'],
                        btn:['确定','取消'],
                        yes:function(index,layero){
                            submitJqueryForm($,index,layero,function(result){
                                if(result.code === 0){
                                    layer.close(indexAdd);
                                    layer.msg('保存成功');
                                    table.reload('lay-table');
                                }else{
                                    layer.alert('保存失败');
                                }
                            });
                        }
                    });
                    break;
                case 'delete':
                    layer.msg('建设中...');
                    break;
                case 'update':
                    layer.msg('建设中...');
                    break;
            }
        });

        //监听模板按钮
        table.on('tool(lay-table)',function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            switch (layEvent) {
                case 'edit':
                    console.log(data.id);
                    var indexEdit = layer.open({
                        type: 2,
                        content: 'edit/' + data.id,
                        maxmin: true,
                        area: ['80%', '80%'],
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            submitJqueryForm($, index, layero, function (result) {
                                if (result.code === 0) {
                                    layer.close(indexEdit);
                                    layer.msg('保存成功');
                                    table.reload('lay-table')
                                } else {
                                    layer.alert('保存失败');
                                }
                                //console.log(result);
                            });
                        },
                    });
                    break;
                case 'delete':
                    layer.confirm('确定删除此角色？',{icon:3,title:'提示'},function(index){
                        $.post("delete/" + data.id, function (result) {
                            if (result.code === 0) {
                                layer.msg('删除成功');
                                tr.remove();
                            } else {
                                layer.alert('删除失败');
                            }
                        });
                    });

                    break;
                default:
                    break;
            }

        });



        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

<!--模板(操作)-->
<script type="text/html" id="table_op">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view"><i
            class="layui-icon layui-icon-about"></i>查看</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
</body>
</html>
